package com.fr.lintcode;

public class Q1851 {

    /**
     * @param arr:
     *            the line
     * @param k:
     *            Alex place
     * @return: the time when Alex requires to buy all passes
     */
    public int buyPasses(int[] arr, int k) {
        // Write your code here.
        if (arr == null || arr.length < k) {
            return 0;
        }
        int cost = 0;
        while (arr[k] > 0) {
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] > 0) {
                    cost++;
                    arr[i]--;
                }
                if (i == k && arr[i] == 0) {
                    break;
                }
            }
        }
        return cost;
    }
}
